package controller

import (
	"cashway.com/cis/assistant/model"
	"cashway.com/cis/assistant/service"
	"cashway.com/cis/assistant/types"
	"github.com/sirupsen/logrus"
)

func TcpRequest(resp *types.ResponseWriter, req *types.Request[model.TcpRequestParams]) {

	if req.IsAsync {
		go func() {
			data, err := service.TcpRequest(&req.ReqBody)
			if err != nil {
				logrus.Error(err)
				return
			}
			logrus.Infof("TcpRequest Remote Resp Data %s", data)
		}()
		resp.RespJsonCommonSuccess()
		return
	}

	data, err := service.TcpRequest(&req.ReqBody)

	if err != nil {
		logrus.Error(err)
		resp.RespJsonCommonFailedMsg(err.Error())
		return
	}

	resp.RespJsonCommonSuccess(string(data))
}
